// pages/park-ticket/parkTicket.js
let request = require("../../utils/request.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    barHeight: wx.getMenuButtonBoundingClientRect().bottom,
    parkId: null,
    listLoading: false,
    btnLoading: false,
    currentPage: 1,
    pageSize: 10,
    pageInfo: { records: [], total: -1 },
    selectedParkTicketId: null,
    infoDialog: false,
    content: null,
    can : 1,
    shopCount:'',
    platformCount:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ 
      parkId: options.parkId,
    })
    // 获取停车券
    this.getParkTicket(1)
    //获取平台停车卷
    this.getPlatformTicket()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 显示详情
   */
  showInfo(e){
    this.setData({ content: e.currentTarget.dataset.content, infoDialog: true })
  },
  //顶部tab切换
  tab(e){
    let can = e.target.dataset.val;
    this.setData({
      can : can
    })
    if(can == 1){
      this.getParkTicket()
    }else{
      this.getPlatformTicket(2)
    }
  },
  /**
   * 获取商户停车券
   */
  getParkTicket(val) {
    request.http(
      '/shop/park-ticket-set/get-park-ticket-by-coordinate', 
      'GET', 
      { parkId: this.data.parkId, pageSize: 1000 },
      1,
      false
    ).then(res => {
      console.log(res);
      if(val == 1){
        if(res.data.data.records.length == 0){
          this.getPlatformTicket(1)
        }
      }
      res.data.data.records.push.apply(this.data.pageInfo.records, res.data.data.records);
      this.setData({ pageInfo: res.data.data, listLoading: false ,shopCount : res.data.data.records.length,can:1})
    })
  },

  /**
   * 领取停车券
   */
  receiveParkTicket(e){
    let parkTicket = e.currentTarget.dataset.parkTicket
    this.setData({ btnLoading: true, selectedParkTicketId: parkTicket.id });
    let url = '';
    let data = {};
    if(this.data.can == 1){
      url = '/customer/ticket/receive-shop-park-ticket';
      data.parkTicketSetId = parkTicket.id;
    }else{
      url = '/customer/ticket/receive-platform-park-ticket';
      data.platformTicketId = parkTicket.id;
    }
    request.http(url, 'POST', data,1,
      false
    ).then(res => {
      console.log(res);
      if(res.data.code == 200){
        wx.showToast({ title: '领取成功' })
        //  剩余数量减一
        console.log(this.data.pageInfo);
        this.data.pageInfo.records.forEach((item, index)=> {
          if(item.id == parkTicket.id){
            let key = "pageInfo.records[" + index + "].remainingQuantity" 
            this.setData({
              [key]: parkTicket.remainingQuantity - 1
            })
          }
        })
      }
      this.setData({ btnLoading: false })
    })
  },
  /**
   * 获取平台停车券
   */
  getPlatformTicket(val) {
    request.http(
      '/platform/platform-ticket/page', 
      'GET', 
      { parkId: this.data.parkId, pageSize: 1000 },
      1,
      false
    ).then(res => {
      console.log(res);
      this.setData({
        platformCount : res.data.data.records.length
      })
      if(val == 1){
        if(res.data.data.records.length == 0){
          this.getParkTicket()
        }else{
          res.data.data.records.push.apply(this.data.pageInfo.records, res.data.data.records);
          this.setData({ 
            pageInfo: res.data.data,
             listLoading: false,
             platformCount : res.data.data.records.length,
             can:2
          })
        }
      }
      if(val == 2){
        res.data.data.records.push.apply(this.data.pageInfo.records, res.data.data.records);
        this.setData({ pageInfo: res.data.data, listLoading: false,platformCount : res.data.data.records.length,can:2 })
      }
    })
  },
  /**
   * 展示适用停车场
   */

  showParks(e){
    let parkTicket = e.currentTarget.dataset.parkTicket;
    console.log(parkTicket);
    console.log(parkTicket.parkNameList);
    console.log(this.data.can);

    if(this.data.can == 1){
      this.infoParkingLot(parkTicket)
    }else{
      if ( parkTicket.parkNameList ) {
        let parkNames = parkTicket.parkNameList
        let ParkingLotList = ''
        parkNames.forEach(item =>{
          // 循环列表
          // console.log(item);
          // console.log(index);
          ParkingLotList += '【'+ item +'】' 
        })
        console.log(ParkingLotList);
        // console.log(parkNames);
        this.setData({ content: ParkingLotList, infoDialog: true })

      }
    }
  },
  
  //商户停车卷获取 适用停车场需要调取接口
  infoParkingLot(parkTicket){
    console.log(parkTicket)
    request.http(
      '//shop/park-ticket-set-park/get-shop-ticket-for-parks', 
      'GET', 
      { shopParkTicketSetId	: parkTicket.id },
      1,
      false
    ).then(res => {
      if(res.data.code == 200){
        console.log(res)
        let parkNames = ''
        res.data.data.forEach((park)=> {
          parkNames += '【' + park.parkName + '】'
        })
        this.setData({ content: parkNames, infoDialog: true })
      }
    })
  },

  /**
   * 关闭弹窗
   */
  closeDialog(){
    this.setData({ infoDialog: false })
  },

  /**
   * 返回
   */
  back(){
    wx.navigateBack({ delta: 1 })
  },

  //拨打商户电话
  callPhone(e){
    console.log(e)
    wx.makePhoneCall({
      phoneNumber: e.target.dataset.phone, //仅为示例，并非真实的电话号码
      fail: function(res) {
      },
    })
  },
})